package com.example.demo.controller;

import com.example.demo.common.annotation.SkipToken;
import com.example.demo.common.ResCode;
import com.example.demo.common.Rs;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.CodeUtil;
import com.example.demo.utils.ExcelUtil;
import com.example.demo.utils.JwtUtil;
import com.google.code.kaptcha.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * .
 *
 * @author Li Zhiyuan
 * @since 2020/8/21
 */

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
//@Api(value = "/hello", description = "User相关api")
@Slf4j
public class UserController extends AbstractController{
    @Resource
    private UserService service;

    @GetMapping(path = "/user")
    public ResponseEntity<Rs<Object>> index() {
        User user = new User();
        user.setName("Adam");
        user.setPwd("123456");
        service.save(user);
        //        ModelAndView modelAndView = new ModelAndView("/index");
//        modelAndView.addObject("count", userMapper.findAll().size());
        return Rs.ok(Rs.json().put("user", user));
    }

    @PostMapping(path = "/login")
    @ApiOperation(value = "登录", notes = "根据用户名和密码登录，返回token")
    @SkipToken
    public ResponseEntity<Rs<Object>> login(HttpServletRequest request,
                                            @RequestParam(value = "name") String userName,
                                            @RequestParam(value = "pwd") String passWord) {
        String msg = "用户不存在";
        User user1 = new User();
        user1.setName(userName);
        User user = service.selectOne(user1);
        if (user != null) {
            if (user.getPwd().equals(passWord)) {
                msg = "登录成功";
                String token = JwtUtil.generateToken(user, 1);
                return Rs.ok(Rs.json().put("msg", msg).put("user", user).put("Authorization", token));

            } else {
                return Rs.fail(ResCode.COMPANY_PASSWORD_ERROR);
            }
        }
        return Rs.fail(msg);
    }

    @GetMapping(path = "/all")
    @ApiOperation(value = "查询所有用户")
    public ResponseEntity<Rs<Object>> all() {
        List<Map<String, Object>> list;
        list = service.selectAll();
        return Rs.ok(list);
    }

    @PostMapping(path = "/download")
    @ApiOperation(value = "导出数据")
    public void download() {
        List list = service.selectAll();
        ExcelUtil.writeWithHead(list, "C:\\ceshi.xls", User.class);
    }


    @PostMapping("/upload")
    @ApiOperation(value = "导入数据")
    @SkipToken
    public void upload(@RequestParam MultipartFile file) throws Exception {
        log.info("导入数据");
        ExcelUtil.readWithHead(file, service);
    }

    //验证码测试
    @RequestMapping("/hello")
    public ResponseEntity<Rs<Object>> HelloWorld(HttpServletRequest request){
        log.info((String) request.getHeader("verifyCodeActual"));
        log.info(CodeUtil.getString(request,"verifyCodeActual"));
        if(!CodeUtil.checkVerifyCode(request)){
            return Rs.fail("验证码错误");
        }
        return Rs.ok();
    }


}










